<?php
class Plugin {
  public $name = 'Video Preview & Popup';
  
  public $folder = 'vdp';
  
  public function data($instance_id) {
    $data = new stdClass();
    $data->folder = $this->folder;
    $data->instance_id = $instance_id;
    
    require_once dirname(__FILE__) . '/../../common.inc.php';
    $cond_vals = new stdClass();
    $cond_vals->c = 't.instance_id = :v1';
    $cond_vals->v = array(':v1' => $instance_id);
    $result = DbUtils::get(DbUtils::createPdoInst(), TABLE_NAME_PREFIX_PLUGIN . $this->folder . '_config', $cond_vals, NULL, NULL, NULL, NULL, NULL);
    
    $config = new stdClass();
    $config->video = '';
    if (count($result->d) > 0) {
      $config = $result->d[0];
    }
    if (!isset($config->id)) {
      $config->track_id = MiscUtils::unique();
      $config->instance_id = $instance_id;
    }
    $data->config = $config;
    
    $cond_vals = new stdClass();
    $cond_vals->c = 't.instance_id = :v1';
    $cond_vals->v = array(':v1' => $instance_id);
    $result = DbUtils::get(DbUtils::createPdoInst(), TABLE_NAME_PREFIX_PLUGIN . $this->folder . '_image', $cond_vals, NULL, NULL, NULL, NULL, NULL);
    
    $data->images = $result->d;
    
    return $data;
  }
  
  public function back($data) {
    $instance_id = $data->instance_id;
    return "<div style=\"margin: 50px 0;\">
              <table cellPadding=\"0\" cellSpacing=\"0\">
                <tr>
                  <td colSpan=\"2\" width=\"100px\" height=\"30px\"><b>Video</b></td>
                </tr>
                <tr>
                  <td colSpan=\"2\" ><textarea cols=\"50\" id=\"$this->folder" . "_$instance_id" . "_video\"></textarea></td>
                </tr>
                <tr>
                  <td></td>
                  <td><input style=\"font-size: 11px;\" type=\"button\" id=\"$this->folder" . "_$instance_id" . "_update\" value=\"Save\"></td>
                </tr>
                <tr>
                  <td colSpan=\"2\" id=\"$this->folder" . "_$instance_id" . "_image\"></td>
                </tr>
              </table>
           " . MiscUtils::getPlutinJs('Vdp', json_encode($data))
           . "
           </div>";
  }
  
  public function front($data) {
    require_once dirname(__FILE__) . '/../../common.inc.php';
    $images = array();
    if (isset($data) && $data != NULL && isset($data->images)) {
      foreach ($data->images as &$image) {
        $images[] = 'http://' . DOMAIN . PATH . 'plugins/' . $this->folder . '/' . $image->link;
      }
    }
    $video = (isset($data) && $data != NULL && isset($data->config) && isset($data->config->video)) ? $data->config->video : '';
    require_once dirname(__FILE__) . '/front.php';
    return vdp_front((count($images) > 0) ? $images[0] : '', $video, $data->instance_id);
  }
  
  public function _convert($obj) {
    return 'http://' . DOMAIN . PATH . 'plugins/' . $this->folder . '/' . $obj->link;
  }
}
?>